﻿using System.Web;
using Microsoft.AspNetCore.Authorization;
using System.IO;
using ff = System.IO;
using Simple.Services.System.FileInfo.Models;
using MiniExcelLibs;
using Simple.Services.System.Market.Models;
using Simple.Services.System.Market;

namespace Simple.WebApi.Controllers.System
{
    /// <summary>
    /// 用户管理
    /// </summary>
    [Route("api/Children/[action]")]
    [ApiController]
    [Authorize]
    public class ChildrenController : ControllerBase
    {
        private readonly ChildrenService _childrenService;

        public ChildrenController(ChildrenService childrenService)
        {
            _childrenService = childrenService;
        }

        /// <summary>
        /// 幼儿列表
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public async Task<AppResult> List()
        {
            List<ChildrenModel> data = await _childrenService.GetAsync();
            return AppResult.Status200OK(data: data);
        }

        /// <summary>
        /// 幼儿查询
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<AppResult> Page([FromQuery] ChildrenPageInputModel model)
        {
            PageResultModel<ChildrenModel> data = await _childrenService.GetPageAsync(model);
            return AppResult.Status200OK(data: data);
        }

        /// <summary>
        /// 幼儿增加
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<AppResult> Add([FromBody] ChildrenInputModel model)
        {
            await _childrenService.AddAsync(model);
            return AppResult.Status200OK("新增成功");
        }

        /// <summary>
        /// 幼儿编辑
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<AppResult> Edit([FromBody] ChildrenInputModel model)
        {
            await _childrenService.UpdateAsync(model);
            return AppResult.Status200OK("更新成功");
        }

        /// <summary>
        /// 幼儿删除
        /// </summary>
        /// <param name="models"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<AppResult> Delete([FromBody] List<IdInputModel> models)
        {
            await _childrenService.DeleteAsync(models.Select(m => m.Id));
            return AppResult.Status200OK("删除成功");
        }

        /// <summary>
        /// 幼儿转班
        /// </summary>
        /// <param name="models"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<AppResult> ShiftTransfers([FromBody] ShiftTransfersDto input)
        {
            await _childrenService.ShiftTransfers(input);
            return AppResult.Status200OK("转班成功");
        }

        /// <summary>
        /// 幼儿离班
        /// </summary>
        /// <param name="models"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<AppResult> LeaveClass([FromBody] IdInputModel models)
        {
            await _childrenService.LeaveClass(models);
            return AppResult.Status200OK("离班成功");
        }

        /// <summary>
        /// 删除家属关系
        /// </summary>
        /// <param name="models"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<AppResult> DeleteRelation([FromBody] DeleteRelationInput models)
        {
            await _childrenService.DeleteRelation(models);
            return AppResult.Status200OK("删除成功");
        }

        /// <summary>
        /// 幼儿导出
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<FileContentResult> ExportChildren([FromQuery] ChildrenPageInputModel model)
        {
            var file = await _childrenService.ExportChildren(model);
            return File(file.FileData, "application/octet-stream", fileDownloadName: HttpUtility.UrlEncode(file.FileName));
        }

        /// <summary>
        /// 导入模板
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        [AllowAnonymous]
        public async Task<FileStreamResult> ImportTemp()
        {
            var fileName = "幼儿导入模板.xlsx";
            string fileDir = Path.Combine(AppContext.BaseDirectory, "TempFiles", fileName);
            var stream = ff.File.OpenRead(fileDir);  //创建文件流
            HttpContext.Response.Headers.Add("Access-Control-Expose-Headers", "Content-Disposition");
            return File(stream, "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", HttpUtility.UrlEncode(fileName));
        }

        /// <summary>
        /// 幼儿导入
        /// </summary>
        /// <param name="inputModel"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<AppResult> ImportChildren([FromForm] FileInfoInputModel inputModel)
        {
            List<ChildrenImportInput> demos = new();
            if (inputModel.File.ContentType != "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet" && inputModel.File.ContentType != "application/vnd.ms-excel")
            {
                throw AppResultException.Status500InternalServerError("仅允许导入xls、xlsx格式文件");
            }

            using (var stream = inputModel.File.OpenReadStream())
            {
                demos = stream.Query<ChildrenImportInput>().ToList();
            }
            demos = demos.Where(x => !string.IsNullOrEmpty(x.Name)).ToList();
            var result = await _childrenService.ImportChildren(demos);
            return AppResult.Status200OK(data: result);
        }

        #region 移动端

        /// <summary>
        /// 幼儿查询(移动端)
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<AppResult> MobilePage([FromQuery] ChildrenPageInputMobileModel model)
        {
            PageResultModel<ChildrenModel> data = await _childrenService.GetPageMobileAsync(model);
            return AppResult.Status200OK(data: data);
        }

        /// <summary>
        /// 幼儿编辑(移动端)
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<AppResult> MobileEdit([FromBody] ChildrenInputModel model)
        {
            await _childrenService.MobileUpdateAsync(model);
            return AppResult.Status200OK("更新成功");
        }

        /// <summary>
        /// 幼儿转班(移动端)
        /// </summary>
        /// <param name="models"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<AppResult> MobileShiftTransfers([FromBody] ShiftTransfersDto input)
        {
            await _childrenService.MobileShiftTransfers(input);
            return AppResult.Status200OK("转班成功");
        }

        /// <summary>
        /// 幼儿离班(移动端)
        /// </summary>
        /// <param name="models"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<AppResult> MobileLeaveClass([FromBody] IdInputModel models)
        {
            await _childrenService.MobileLeaveClass(models);
            return AppResult.Status200OK("离班成功");
        }

        /// <summary>
        /// 家属修改(移动端)
        /// </summary>
        /// <param name="models"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<AppResult> MobileUpdateParentRelation([FromBody] MobileUpdateParentRelationDto models)
        {
            await _childrenService.MobileUpdateParentRelation(models);
            return AppResult.Status200OK("家属修改成功");
        }

        /// <summary>
        /// 头像修改(移动端)
        /// </summary>
        /// <param name="models"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<AppResult> MobileUpdateHeadshot([FromBody] MobileUpdateHeadshotInput models)
        {
            await _childrenService.MobileUpdateHeadshot(models);
            return AppResult.Status200OK("修改成功");
        }

        /// <summary>
        /// 获取孩子信息(移动端)
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public async Task<AppResult> MobileChildInfo()
        {
            var data = await _childrenService.MobileChildInfo();
            return AppResult.Status200OK(data: data);
        }

        /// <summary>
        /// 批量上传幼儿头像
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<AppResult> BatchUploadAvatar([FromForm] ChildrenAvatarDto dto)
        {
            await _childrenService.BatchUploadAvatarAsync(dto);
            return AppResult.Status200OK("上传成功");
        }
        #endregion
    }
}
